package socket

import (
	"fmt"
	"github.com/goccy/go-json"
	"reflect"
	models2 "zsgGin/utils/socket/models"
)

const (
	NewPOD=1 //新增POD
	NewUser=2 //user 获取
)
var CommandModel=map[int]models2.IModel{}

func init() {
	CommandModel[NewPOD]=(*models2.PodModel)(nil)
	CommandModel[NewUser]=(*models2.User)(nil)
}
type WsCommand struct {
	CmdType int
	CmdData map[string]interface{}
	CmdAction string
}
func(w *WsCommand) Parse() (*models2.WsResponse,error) {
	if v,ok:= CommandModel[w.CmdType];ok{
		newObj:= reflect.New(reflect.TypeOf(v).Elem()).Interface()
		//map 转换成JSON
		b,_:=json.Marshal(w.CmdData)
		//json 转换成结构体
		err:=json.Unmarshal(b,newObj)
		if err!=nil{
			return nil,err
		}
		return newObj.(models2.IModel).ParseAction(w.CmdAction)
	}
	return nil,fmt.Errorf("error command")
}
